//
//  PaperOptionWebCell.m
//  DoingPaper
//
//  Created by zhuxuhong on 16/8/1.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "PaperOptionWebCell.h"

@interface PaperOptionWebCell()<UIWebViewDelegate>

@property(nonatomic,strong)UIImageView *radioIcon;
@property(nonatomic,strong)UIImageView *contentImageView;
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,weak)id<OptionWebCellDelegate> delegate;

@end

@implementation PaperOptionWebCell
{
    CGFloat off10;
    CGFloat off20;
    PaperQuestionOption *_option;
    BOOL _single;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        self.backgroundColor = [UIColor yellowColor];
//        self.selectedBackgroundView = [[UIView alloc]initWithFrame:self.bounds];
//        self.selectedBackgroundView.backgroundColor = [UIColor redColor];
        
        off10 = 10;
        off20 = 20;
        
        UIWebView *web = [UIWebView new];
        web.scrollView.scrollEnabled = false;
        web.scrollView.contentInset = UIEdgeInsetsZero;
        web.delegate = self;
        _webView = web;
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _webView.frame = CGRectMake(0, 0, self.bounds.size.width, 44);
    [self addSubview: self.radioIcon];
    [self addSubview: self.contentImageView];
}

-(void)setOptionImage:(UIImage *)image{
    if (image) {
        _contentImageView.image = image;
        CGRect frame = _contentImageView.frame;
        frame.size.height = image.size.height;
        _contentImageView.frame = frame;
    }
}

-(UIImageView *)radioIcon{
    if (!_radioIcon) {
        CGRect frame = CGRectMake(off20, CGRectGetHeight(self.bounds)/2-23/2.0, 23, 23);
        UIImageView *iv = [[UIImageView alloc]initWithFrame: frame];
        _radioIcon = iv;
    }
    return _radioIcon;
}

-(UIImageView*)contentImageView{
    if (!_contentImageView) {
        UIImageView *iv = [[UIImageView alloc]initWithFrame: CGRectMake(CGRectGetMaxX(_radioIcon.frame)+off10, 0, CGRectGetWidth(self.bounds)-CGRectGetMaxX(_radioIcon.frame)-off10-off20, CGRectGetHeight(self.bounds))];
        
        _contentImageView = iv;
    }
    return _contentImageView;
}

#pragma mark - 加载html
-(void)loadWebOptionContentWithBaseUrl: (NSString*)url
                                option: (PaperQuestionOption*)option
                              isSingle:(BOOL)single
                              delegate:(id)delegate{
    _option = option;
    _delegate = delegate;
    _single = single;
    
    NSURL *baseURL = [NSURL fileURLWithPath:url];
    [_webView loadHTMLString:_option.text baseURL:baseURL];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return !_option.image;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (webView == _webView) {
        CGFloat h = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];
        if (h > 0) {
            CGRect frame = webView.frame;
            frame.size.height = h * 2;
            webView.frame = frame;
            
            CGRect f = _contentImageView.frame;
            f.size.height = h * 2;
            _contentImageView.frame = f;
            
            CGRect r = _radioIcon.frame;
            r.origin.y = CGRectGetHeight(f)/2-23/2.0;
            _radioIcon.frame = r;
            
            // 生成image
            _option.image = [self viewRenderToImage: webView];
            [_delegate optionWebCellContentDidLoadWithOption: _option];
        }
    }
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
}

/**
 *  UIView to UIImage
 */
-(UIImage*)viewRenderToImage: (UIView*)view{
    CGSize size = view.bounds.size;
    UIGraphicsBeginImageContextWithOptions(size, false, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

-(void)setChoosed:(BOOL)choosed{ //用自定义属性 控制icon，selected不好用
    NSString *flag = _single ? @"single" : @"multi";
    NSString *choose = choosed ? @"sel" : @"normal";
    _radioIcon.image = [UIImage imageNamed:
                        [NSString stringWithFormat:@"radio_%@_%@",flag,choose]];
    _choosed = choosed;
}

@end
